﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Webdiyer.WebControls.Mvc;
using YiYiTong.Rocket.Helper;
using YiYiTong.Rocket.EFDBFirst;
using YiYiTong.Rocket.Model;
using System.ComponentModel;
using YiYiTong.Rocket.Model.Manager;
using YiYiTong.Rocket.Business;

namespace YiYiTong.Rocket.ManageWeb.Controllers
{
    public class ChargeOrderController : BaseController
    {
        [Description("充值列表")]
        [ViewPage]
        public ActionResult GetList(ChargeOrderQueryModel queryModel)
        {
            queryModel.Trim();
            ViewBag.paramModel = queryModel;

            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            //条件筛选
            var queryable = dbcontext.View_ChargeOrder.Where(t => t.is_del == 0);
            if (!string.IsNullOrWhiteSpace(queryModel.account_mobile))
                queryable = queryable.Where(t => t.account_mobile == queryModel.account_mobile);
            if (!string.IsNullOrWhiteSpace(queryModel.member_name))
                queryable = queryable.Where(t => t.member_name.Contains(queryModel.member_name));
            if (!string.IsNullOrWhiteSpace(queryModel.replace_buyer_NO))
                queryable = queryable.Where(t => t.replace_buyer_NO == queryModel.replace_buyer_NO);
            if (!string.IsNullOrWhiteSpace(queryModel.order_no))
                queryable = queryable.Where(t => t.order_no.Contains(queryModel.order_no));
            if (!string.IsNullOrWhiteSpace(queryModel.order_master_no))
                queryable = queryable.Where(t => t.order_master_no.Contains(queryModel.order_master_no));
            if (queryModel.status != -10)
                queryable = queryable.Where(t => t.status == queryModel.status);
            if (queryModel.issuccess != -10)
                queryable = queryable.Where(t => t.issuccess == queryModel.issuccess);
            if (queryModel.channel_type != -10)
                queryable = queryable.Where(t => t.channel_type == queryModel.channel_type);
            if (queryModel.beg_time.HasValue)
                queryable = queryable.Where(t => t.createtime >= queryModel.beg_time);
            if (queryModel.end_time.HasValue)
            {
                var end_time = queryModel.end_time.Value.AddSeconds(1);
                queryable = queryable.Where(t => t.createtime < end_time);
            }
            if (queryModel.beg_succ_time.HasValue)
                queryable = queryable.Where(t => t.succ_time >= queryModel.beg_succ_time);
            if (queryModel.end_succ_time.HasValue)
            {
                var end_succ_time = queryModel.end_succ_time.Value.AddSeconds(1);
                queryable = queryable.Where(t => t.succ_time < end_succ_time);
            }

            //分页
            int rowcounts = queryable.Count();
            ViewData.Add("AllCount", rowcounts);
            decimal? orderAmt = queryable.Sum(t => t.order_amt);
            ViewData.Add("AllOrderAmt", orderAmt);
            var querylist = queryable.OrderByDescending(t => t.order_id).ThenByDescending(t => t.createtime)
                .Skip(queryModel.pageSize * (queryModel.pageIndex - 1))
                .Take(queryModel.pageSize)
                .ToList();

            PagedList<View_ChargeOrder> pagedlist = null;
            if (querylist != null && querylist.Count != 0)
                pagedlist = new PagedList<View_ChargeOrder>(querylist, queryModel.pageIndex, queryModel.pageSize, rowcounts);
            return View(pagedlist);
        }

        [Description("充值详情")]
        [ViewPage]
        public ActionResult Defail(long order_id)
        {
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            View_ChargeOrder model = new View_ChargeOrder();
            if (order_id > 0)
            {
                model = dbcontext.View_ChargeOrder.FirstOrDefault(t => t.order_id == order_id && t.is_del == 0);
            }
            return View(model);
        }
    }
}